package com.x.attendance.assemble.control.service;

import java.util.List;

import com.x.attendance.core.entity.AttendanceDevice;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;

/**
 * 中控Live20指纹采集器服务
 * @author Manus AI
 */
public class FingerprintCollectorService {

	private static Logger logger = LoggerFactory.getLogger(FingerprintCollectorService.class);
	
	private boolean deviceInitialized = false;
	private ZKTecoSDKWrapper sdkWrapper;

	public FingerprintCollectorService() {
		this.sdkWrapper = new ZKTecoSDKWrapper();
	}

	/**
	 * 初始化指纹采集器
	 */
	public boolean initializeDevice() {
		try {
			if (deviceInitialized) {
				return true;
			}
			
			boolean result = sdkWrapper.initializeCollector();
			if (result) {
				deviceInitialized = true;
				logger.info("指纹采集器初始化成功");
			} else {
				logger.error("指纹采集器初始化失败");
			}
			
			return result;
		} catch (Exception e) {
			logger.error("指纹采集器初始化异常", e);
			return false;
		}
	}

	/**
	 * 采集指纹
	 */
	public FingerprintData collectFingerprint() {
		try {
			if (!deviceInitialized) {
				if (!initializeDevice()) {
					return null;
				}
			}

			// 调用SDK采集指纹
			FingerprintData data = sdkWrapper.collectFingerprint();
			
			if (data != null && data.getQuality() >= 60) { // 质量阈值
				logger.info("指纹采集成功，质量: " + data.getQuality());
				return data;
			} else {
				logger.warn("指纹质量不达标或采集失败");
				return null;
			}
			
		} catch (Exception e) {
			logger.error("指纹采集异常", e);
			return null;
		}
	}

	/**
	 * 获取指纹图像
	 */
	public byte[] getFingerprintImage() {
		try {
			if (!deviceInitialized) {
				return null;
			}
			
			return sdkWrapper.getFingerprintImage();
		} catch (Exception e) {
			logger.error("获取指纹图像异常", e);
			return null;
		}
	}

	/**
	 * 关闭设备
	 */
	public void closeDevice() {
		try {
			if (deviceInitialized) {
				sdkWrapper.closeDevice();
				deviceInitialized = false;
				logger.info("指纹采集器已关闭");
			}
		} catch (Exception e) {
			logger.error("关闭指纹采集器异常", e);
		}
	}

	/**
	 * 检查设备状态
	 */
	public boolean isDeviceReady() {
		return deviceInitialized && sdkWrapper.isDeviceConnected();
	}

	/**
	 * 指纹数据类
	 */
	public static class FingerprintData {
		private byte[] template;
		private byte[] image;
		private int quality;
		private boolean success;

		public byte[] getTemplate() {
			return template;
		}

		public void setTemplate(byte[] template) {
			this.template = template;
		}

		public byte[] getImage() {
			return image;
		}

		public void setImage(byte[] image) {
			this.image = image;
		}

		public int getQuality() {
			return quality;
		}

		public void setQuality(int quality) {
			this.quality = quality;
		}

		public boolean isSuccess() {
			return success;
		}

		public void setSuccess(boolean success) {
			this.success = success;
		}
	}

	/**
	 * 中控SDK包装类
	 */
	private static class ZKTecoSDKWrapper {
		
		static {
			try {
				// 加载中控SDK库
				System.loadLibrary("zkfinger");
				logger.info("中控SDK库加载成功");
			} catch (UnsatisfiedLinkError e) {
				logger.error("中控SDK库加载失败: " + e.getMessage());
			}
		}

		public boolean initializeCollector() {
			// 调用native方法初始化设备
			return nativeInitDevice();
		}

		public FingerprintData collectFingerprint() {
			FingerprintData data = new FingerprintData();
			
			// 调用native方法采集指纹
			byte[] template = new byte[2048];
			byte[] image = new byte[73728]; // 256*288
			int[] quality = new int[1];
			
			boolean success = nativeCollectFingerprint(template, image, quality);
			
			data.setSuccess(success);
			if (success) {
				data.setTemplate(template);
				data.setImage(image);
				data.setQuality(quality[0]);
			}
			
			return data;
		}

		public byte[] getFingerprintImage() {
			byte[] image = new byte[73728];
			boolean success = nativeGetFingerprintImage(image);
			return success ? image : null;
		}

		public void closeDevice() {
			nativeCloseDevice();
		}

		public boolean isDeviceConnected() {
			return nativeIsDeviceConnected();
		}

		// Native方法声明
		private native boolean nativeInitDevice();
		private native boolean nativeCollectFingerprint(byte[] template, byte[] image, int[] quality);
		private native boolean nativeGetFingerprintImage(byte[] image);
		private native void nativeCloseDevice();
		private native boolean nativeIsDeviceConnected();
	}
}

